package main

import (
	"context"
	"fmt"
	"go.uber.org/zap"
	"google.golang.org/grpc"
	"google.golang.org/grpc/credentials/insecure"
	"mxshop_srvs/inventory_srv/proto"
	"sync"
)

var Client proto.InventoryClient
var Conn *grpc.ClientConn

/*
	func TestInvDetail(goodsId int32) {
		rsp, err := Client.InvDetail(context.Background(), &proto.GoodsInvInfo{
			GoodsId: goodsId,
		})
		if err != nil {
			panic(err)
		}
		fmt.Println(rsp.Num)
	}
*/
func TestSell(wg *sync.WaitGroup) {
	defer wg.Done()
	_, err := Client.Sell(context.Background(), &proto.SellInfo{
		GoodsInfo: []*proto.GoodsInvInfo{{
			GoodsId: 1,
			Num:     1,
		},
		},
	})
	if err != nil {
		//panic(err)
		fmt.Printf("销售失败:%v", err)
		fmt.Println()
		return
	}
	fmt.Println("扣减成功")
}

/*
	func TestReback() {
		_, err := Client.Reback(context.Background(), &proto.SellInfo{
			GoodsInfo: []*proto.GoodsInvInfo{{
				GoodsId: 1,
				Num:     10,
			}, {
				GoodsId: 2,
				Num:     20,
			},
			},
		})
		if err != nil {
			panic(err)
		}
		fmt.Println("归还成功")
	}
*/

/*
	func TestSetInv(goodsId int32, num int32) {
		_, err := Client.SetInv(context.Background(), &proto.GoodsInvInfo{
			GoodsId: goodsId,
			Num:     num,
		})
		if err != nil {
			panic(err)
		}
	}
*/
func Init() {
	var err error
	Conn, err = grpc.NewClient("192.168.175.1:58626", grpc.WithTransportCredentials(insecure.NewCredentials()))
	if err != nil {
		panic(err)
	}
	Client = proto.NewInventoryClient(Conn)
}
func main() {
	Init()
	var i int32
	wg := sync.WaitGroup{}
	wg.Add(100)
	for i = 0; i < 100; i++ {
		go TestSell(&wg)
	}
	wg.Wait()

	defer func() {
		err := Conn.Close()
		if err != nil {
			zap.S().Errorf("conn.close错误")
		}
	}()

}
